package com.zahb.qadx.ui.activity.videopage;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.githang.statusbar.StatusBarCompat;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.model.MainFunc2;
import com.zahb.qadx.model.VideoListBean;
import com.zahb.qadx.util.DisplayUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MicroVideoPlaybackActivity extends BaseActivity implements
        OnItemClickListener, MicroVideoPlayerFragment.onFullHalfViewClickListener {

    @BindView(R.id.container_layout)
    ConstraintLayout mContainerLayout;
    @BindView(R.id.media_container)
    FrameLayout mMediaContainer;
    private ConstraintSet mPortraitConstraintSet;
    private ConstraintSet mLandscapeConstraintSet;
    private View mDecorView;
    private int mDefaultOption;
    private int mNavigationBarColor;

    private MicroVideoPlayerFragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            mTopBarContainer.setVisibility(View.VISIBLE);
            mTopBarDivider.setVisibility(View.VISIBLE);
            mPortraitConstraintSet.applyTo(mContainerLayout);
            mDecorView.setSystemUiVisibility(mDefaultOption);
            getWindow().setNavigationBarColor(mNavigationBarColor);
            StatusBarCompat.setStatusBarColor(this, Color.WHITE);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            mTopBarContainer.setVisibility(View.GONE);
            mTopBarDivider.setVisibility(View.GONE);
            mLandscapeConstraintSet.applyTo(mContainerLayout);
            int option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            mDecorView.setSystemUiVisibility(option);
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
            StatusBarCompat.setStatusBarColor(this, Color.TRANSPARENT);
        }
    }

    @Override
    public void onBackPressed() {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_micro_video_playback;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);
        VideoListBean bean = (VideoListBean) getIntent().getSerializableExtra("bean");
        if (bean != null) {
//            mTvTitle.setText(bean.getName());//标题
            initRecyclerView();
            replaceMediaFragment(bean.getSourceId());

        }
        initConstraintSets();
        mDecorView = getWindow().getDecorView();
        mDefaultOption = mDecorView.getSystemUiVisibility();
        mNavigationBarColor = getWindow().getNavigationBarColor();
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.video_details;
    }


    private void initRecyclerView() {

        final int divider_h = DisplayUtil.dip2px2(5.5f);
        final int divider_v = DisplayUtil.dip2px(16.5f);
        RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                if (parent.getAdapter() == null) return;
                outRect.set(0, 0, 0, 0);
                if (parent.getChildAdapterPosition(view) % 2 == 0) {// 左边
                    outRect.right = divider_h;
                } else if (parent.getChildAdapterPosition(view) % 2 == 1) {// 右边
                    outRect.left = divider_h;
                }
                if (parent.getChildAdapterPosition(view) >= 2) {
                    outRect.top = divider_v;
                }
            }
        };

    }

    private void initConstraintSets() {
        mPortraitConstraintSet = new ConstraintSet();
        mPortraitConstraintSet.clone(mContainerLayout);
        mLandscapeConstraintSet = new ConstraintSet();
        mLandscapeConstraintSet.clone(mContainerLayout);
        mLandscapeConstraintSet.clear(R.id.media_container);
        mLandscapeConstraintSet.connect(R.id.media_container, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
        mLandscapeConstraintSet.connect(R.id.media_container, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
        mLandscapeConstraintSet.connect(R.id.media_container, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        mLandscapeConstraintSet.connect(R.id.media_container, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
    }




    private void replaceMediaFragment(String sourceId) {
        boolean isAudio = false;
        if (mFragment == null) {
            mFragment = MicroVideoPlayerFragment.newInstance(sourceId, isAudio);
            mFragment.setOnFullHalfViewClickListener(MicroVideoPlaybackActivity.this);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.media_container, mFragment)
                    .commitAllowingStateLoss();
        } else {
            mFragment.changeMedia(sourceId, isAudio);
        }
    }

    public static final String TYPE_VIDEO = "video";

    public static void actionStart(Context context, VideoListBean bean, String type) {
        Intent intent = new Intent(context, MicroVideoPlaybackActivity.class);
        intent.putExtra("bean", bean);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    public static void actionStart(Context context, MainFunc2 mainFunc2, String type) {
        VideoListBean bean = new VideoListBean();
        bean.setCovenImage(mainFunc2.getIcon());
        bean.setName(mainFunc2.getTitle());
        bean.setId(mainFunc2.getSourceId());
        actionStart(context, bean, type);
    }



    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

    }

    @Override
    public void onRequestFull(ImageView view) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    @Override
    public void onRequestHalf(ImageView view) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}




















